<html>
<head><meta charset="utf-8"><title>why do we invoke the collector twice? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html">why do we invoke the collector twice?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="152567672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/152567672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#152567672">(Dec 26 2018 at 19:26)</a>:</h4>
<p>So <span class="user-mention" data-user-id="119009">@eddyb</span>, <span class="user-mention" data-user-id="124287">@mw</span>, or somebody, perhaps one of you can explain <a href="https://github.com/rust-lang/rust/blob/79d8a0fcefa5134db2a94739b1d18daa01fc6e9f/src/librustc_codegen_utils/codegen_backend.rs#L128-L132" target="_blank" title="https://github.com/rust-lang/rust/blob/79d8a0fcefa5134db2a94739b1d18daa01fc6e9f/src/librustc_codegen_utils/codegen_backend.rs#L128-L132">this code</a> to me:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">codegen_crate</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;tcx</span><span class="o">&gt;</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">tcx</span>: <span class="nc">TyCtxt</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;tcx</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_rx</span>: <span class="nc">mpsc</span>::<span class="n">Receiver</span><span class="o">&lt;</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">Any</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="o">&gt;&gt;</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">Any</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">...</span><span class="w"></span>
<span class="w">    </span>::<span class="n">rustc_mir</span>::<span class="n">monomorphize</span>::<span class="n">assert_symbols_are_distinct</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">collector</span>::<span class="n">collect_crate_mono_items</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="n">tcx</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">collector</span>::<span class="n">MonoItemCollectionMode</span>::<span class="n">Eager</span><span class="w"></span>
<span class="w">            </span><span class="p">).</span><span class="mf">0.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">...</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">mono_item</span><span class="w"> </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="n">collector</span>::<span class="n">collect_crate_mono_items</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="n">tcx</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">collector</span>::<span class="n">MonoItemCollectionMode</span>::<span class="n">Eager</span><span class="w"></span>
<span class="w">            </span><span class="p">).</span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="p">...</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>unless i'm missing something, won't this run the collector <em>twice</em>?</p>



<a name="152567683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/152567683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#152567683">(Dec 26 2018 at 19:27)</a>:</h4>
<p>I would expect a query or something</p>



<a name="152567687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/152567687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#152567687">(Dec 26 2018 at 19:27)</a>:</h4>
<p>maybe that code is dead or something? (not obviously true)</p>



<a name="154557073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154557073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154557073">(Jan 07 2019 at 09:50)</a>:</h4>
<p>Yes, that just looks wrong. It's probably meant to call the <code>collect_and_partition_mono_items</code> query.</p>



<a name="154578386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154578386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154578386">(Jan 07 2019 at 15:50)</a>:</h4>
<p>should we file an issue <span class="user-mention" data-user-id="124287">@mw</span> ?</p>



<a name="154579006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154579006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154579006">(Jan 07 2019 at 15:58)</a>:</h4>
<p>yes</p>



<a name="154579206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154579206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154579206">(Jan 07 2019 at 16:01)</a>:</h4>
<p><span class="user-mention" data-user-id="124287">@mw</span> filed <a href="https://github.com/rust-lang/rust/issues/57406" target="_blank" title="https://github.com/rust-lang/rust/issues/57406">https://github.com/rust-lang/rust/issues/57406</a></p>



<a name="154581026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154581026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154581026">(Jan 07 2019 at 16:26)</a>:</h4>
<p><span class="emoji emoji-1f44d" title="+1">:+1:</span></p>



<a name="154593017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154593017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154593017">(Jan 07 2019 at 19:19)</a>:</h4>
<p>are there specific tests to run for this issue apart from the codegen ones ? mostly I'm wondering if changing the collector mode from eager to "whatever the query is doing" (that is, lazy unless we asked to link dead code) will need special care ?</p>



<a name="154593114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154593114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154593114">(Jan 07 2019 at 19:20)</a>:</h4>
<p>(I guess I can also open a WIP PR and see what travis complains about)</p>



<a name="154595475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154595475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154595475">(Jan 07 2019 at 19:46)</a>:</h4>
<blockquote>
<p>I guess I can also open a WIP PR</p>
</blockquote>
<p><a href="https://github.com/rust-lang/rust/issues/57418" target="_blank" title="https://github.com/rust-lang/rust/issues/57418">#57418</a><br>
(and thanks <span class="user-mention" data-user-id="123586">@nagisa</span> I wondered whether I should have mentioned  in the PR description the reason for removing the <code>assert_symbols_are_distinct</code>)</p>



<a name="154596728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154596728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154596728">(Jan 07 2019 at 20:00)</a>:</h4>
<p>Never hurts to mention it, so that a reviewer doesn’t need to go and verify <span class="emoji emoji-1f642" title="slight smile">:slight_smile:</span></p>



<a name="154650848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154650848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154650848">(Jan 08 2019 at 15:14)</a>:</h4>
<p>turns out this is an <a href="https://github.com/rust-lang/rust/pull/57418#issuecomment-452323034" target="_blank" title="https://github.com/rust-lang/rust/pull/57418#issuecomment-452323034">unimportant codegen backend</a> not the LLVM one, some comments might have saved us all the confusion :)</p>



<a name="154654383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154654383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154654383">(Jan 08 2019 at 16:06)</a>:</h4>
<p>I realized that already, still seems bad</p>



<a name="154654396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154654396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154654396">(Jan 08 2019 at 16:06)</a>:</h4>
<p>but if I thought it was on the main code path I would have made a bigger deal of it :)</p>



<a name="154654904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154654904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154654904">(Jan 08 2019 at 16:13)</a>:</h4>
<p>I wonder if there are tests for this specific code path</p>



<a name="154664429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154664429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154664429">(Jan 08 2019 at 18:17)</a>:</h4>
<p>(apparently so, nice)</p>



<a name="154711358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154711358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154711358">(Jan 09 2019 at 09:40)</a>:</h4>
<p><span class="user-mention" data-user-id="124287">@mw</span> what do you want me to do for <a href="https://github.com/rust-lang/rust/issues/57418" target="_blank" title="https://github.com/rust-lang/rust/issues/57418">#57418</a> ? I'm not sure how to test this backend (it seems to be used in one <code>run-make-fulldeps</code> test) to make sure the behaviour doesn't change (and bjorn3 thought it was broken). I was thinking: 1) remove the <code>inst.def.is_inline(tcx)</code> calls you mentioned 2) add a comment explaining what this backend does (apparently it's part of a bigger picture to allow rustc to be built without LLVM)</p>



<a name="154715990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154715990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154715990">(Jan 09 2019 at 11:03)</a>:</h4>
<p>I'm not sure what this backend is used for, either. I think even the RLS needs LLVM at the moment.</p>



<a name="154716033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154716033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154716033">(Jan 09 2019 at 11:04)</a>:</h4>
<p>I'd say, if the <code>run-make-fulldeps</code> tests pass, it should be fine.</p>



<a name="154716061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154716061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154716061">(Jan 09 2019 at 11:04)</a>:</h4>
<p>I'd also start with removing the <code>is_inline</code> calls and see if that breaks any tests.</p>



<a name="154721856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154721856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154721856">(Jan 09 2019 at 13:08)</a>:</h4>
<p>hmm bjorn3 says this backend is not really used</p>



<a name="154721879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154721879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mw <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154721879">(Jan 09 2019 at 13:09)</a>:</h4>
<p>OK, in that case, anything that doesn't break tests is fine, I'd say.</p>



<a name="154722172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20do%20we%20invoke%20the%20collector%20twice%3F/near/154722172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20do.20we.20invoke.20the.20collector.20twice.3F.html#154722172">(Jan 09 2019 at 13:15)</a>:</h4>
<p>I've removed the is_inline call, and it doesn't change the <code>run-make-fulldeps</code> tests locally; I'll push the commit so that we can check Travis (update: commit pushed)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>